package com.ikimuhendis.ldrawer;

import ohos.agp.components.Component;
import ohos.agp.components.element.Element;
import ohos.agp.render.Canvas;
import ohos.agp.render.ColorFilter;
import ohos.agp.render.Paint;
import ohos.agp.render.Path;
import ohos.agp.utils.Color;
import ohos.agp.utils.Rect;
import ohos.app.Context;

/**
 * DrawerArrowDrawable
 *
 * @since 2021-07-12
 */
public abstract class DrawerArrowDrawable extends Element implements Component.DrawTask {
    private static final float ARROW_HEAD_ANGLE = (float) Math.toRadians(45.0D);
    private static final int INT_50 = 50;
    private static final int INT_18 = 18;
    private static final int INT_11 = 11;
    private static final int INT_2 = 2;
    private static final int INT_3 = 3;
    private static final int INT_16 = 16;
    private static final int INT_1 = -1;
    private static final float FLOAT_180 = 180.0F;
    private static final float FLOAT_2 = 2.0F;
    protected float mBarGap;
    protected float mBarSize;
    protected float mBarThickness;
    protected float mMiddleArrowSize;
    protected final Paint mPaint = new Paint();
    protected final Path mPath = new Path();
    protected float mProgress;
    protected int mSize;
    protected float mVerticalMirror = 1f;
    protected float mTopBottomArrowSize;
    protected Context context;

    /**
     * DrawerArrowDrawable
     *
     * @param context
     */
    public DrawerArrowDrawable(Context context) {
        this.context = context;
        this.mPaint.setAntiAlias(true);
        this.mPaint.setColor(Color.WHITE);
        this.mSize = INT_50;
        this.mBarSize = INT_18;
        this.mTopBottomArrowSize = INT_11;
        this.mBarThickness = INT_2;
        this.mBarGap = INT_3;
        this.mMiddleArrowSize = INT_16;
        this.mPaint.setStyle(Paint.Style.STROKE_STYLE);
        this.mPaint.setStrokeJoin(Paint.Join.ROUND_JOIN);
        this.mPaint.setStrokeCap(Paint.StrokeCap.SQUARE_CAP);
        this.mPaint.setStrokeWidth(this.mBarThickness);
    }

    /**
     * lerp
     *
     * @param paramFloat1
     * @param paramFloat2
     * @param paramFloat3
     * @return float
     */
    protected float lerp(float paramFloat1, float paramFloat2, float paramFloat3) {
        return paramFloat1 + paramFloat3 * (paramFloat2 - paramFloat1);
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        Rect localRect = getBounds();
        float f1 = lerp(this.mBarSize, this.mTopBottomArrowSize, this.mProgress);
        float f2 = lerp(this.mBarSize, this.mMiddleArrowSize, this.mProgress);
        float f3 = lerp(0.0F, this.mBarThickness / FLOAT_2, this.mProgress);
        float f4 = lerp(0.0F, ARROW_HEAD_ANGLE, this.mProgress);
        float f8 = lerp(this.mBarGap + this.mBarThickness, 0.0F, this.mProgress);
        this.mPath.rewind();
        float f9 = -f2 / FLOAT_2;
        this.mPath.moveTo(f9 + f3, 0.0F);
        this.mPath.rLineTo(f2 - f3, 0.0F);
        float f10 = (float) Math.round(f1 * Math.cos(f4));
        float f11 = (float) Math.round(f1 * Math.sin(f4));
        this.mPath.moveTo(f9, f8);
        this.mPath.rLineTo(f10, f11);
        this.mPath.moveTo(f9, -f8);
        this.mPath.rLineTo(f10, -f11);
        this.mPath.moveTo(0.0F, 0.0F);
        this.mPath.close();
        canvas.save();
        if (!isLayoutRtl()) {
            canvas.rotate(FLOAT_180, localRect.getCenterX(), localRect.getCenterY());
        }
        float f5 = 0.0F;
        float f6 = FLOAT_180;
        float f7 = lerp(f5, f6, this.mProgress);
        canvas.rotate(f7 * mVerticalMirror, localRect.getCenterX(), localRect.getCenterY());
        canvas.translate(localRect.getCenterX(), localRect.getCenterY());
        canvas.drawPath(this.mPath, this.mPaint);
        canvas.restore();
    }

    /**
     * getIntrinsicHeight
     *
     * @return int
     */
    public int getIntrinsicHeight() {
        return this.mSize;
    }

    /**
     * getIntrinsicWidth
     *
     * @return int
     */
    public int getIntrinsicWidth() {
        return this.mSize;
    }

    /**
     * setAlpha
     *
     * @param alpha
     */
    public void setAlpha(int alpha) {
        this.mPaint.setAlpha(alpha);
    }

    /**
     * isLayoutRtl
     *
     * @return boolean
     */
    public abstract boolean isLayoutRtl();

    /**
     * setColorFilter
     *
     * @param colorFilter
     */
    public void setColorFilter(ColorFilter colorFilter) {
        this.mPaint.setColorFilter(colorFilter);
    }

    /**
     * setVerticalMirror
     *
     * @param isVerticalMirror
     */
    public void setVerticalMirror(boolean isVerticalMirror) {
        this.mVerticalMirror = isVerticalMirror ? 1 : INT_1;
    }

    /**
     * setProgress
     *
     * @param paramFloat
     */
    public void setProgress(float paramFloat) {
        this.mProgress = paramFloat;
    }

    /**
     * setColor
     *
     * @param resourceId
     */
    public void setColor(int resourceId) {
        this.mPaint.setColor(new Color(resourceId));
    }
}